//
// Created by liugd on 24-7-19.
//
#include "../headers/common.h"
#include "../headers/account_item.h"
#include <functional>
#include <cstring>



void loadDataFormFile(vector<AccountItem>& items) {

    ifstream input(FILENAME);
    //逐行读取
    AccountItem item;
    while (input >> item.itemType >> item.amount >> item.detail) {
        items.push_back(item);
    }
    input.close();
}

//记账
void accounting(vector<AccountItem>& items) {
    bool quit = false;
    while (!quit) {
        //键盘选择
        char key = readMenuSelection(3);
        switch (key) {
            case '1':
                //入账
                income(items);
                break;
            case '2':
                //支出
                expand(items);
                break;
            case '3':
                cout << "\n确认退出？(Y/N)";
                if (readQuitConfirm()) quit = true;
                break;
            default:
                break;
        }
        cout << endl;
    }
}

//入账
void income(vector<AccountItem>& items) {

    AccountItem item;
    item.itemType = INCOME;
    cout << "\n本次收入金额：";
    item.amount = readAmount();
    cout << "\n备注：";
    getline(cin, item.detail);

    items.push_back(item);
    //写入文件
    insertDataToFile(item);

    cout << "记账成功！" << endl;
    cout << "按回车键返回主菜单" << endl;

    string line;
    getline(cin, line);

}

//支出
void expand(vector<AccountItem>& items) {

    AccountItem item;
    item.itemType = EXPAND;
    cout << "\n本次支出金额：";
    item.amount = -readAmount();
    cout << "\n备注：";
    getline(cin, item.detail);

    items.push_back(item);
    //写入文件
    insertDataToFile(item);

    cout << "记账成功！" << endl;
    cout << "按回车键返回主菜单" << endl;

    string line;
    getline(cin, line);

}

void insertDataToFile(AccountItem& item) {
    ofstream output(FILENAME, ios::out | ios::app);
    output << item.itemType << "\t" << item.amount << "\t" << item.detail << endl;
    output.close();
}



//查询
void query(const vector<AccountItem>& items) {
    bool quit = false;
    while (!quit) {
        //键盘选择
        char key = readMenuSelection(4);
        switch (key) {
            case '1':
                //查询所有账目，统计总收支
                queryItems(items);
                break;
            case '2':
                //统计总收入
                queryItems(items, INCOME);
                break;
            case '3':
                //统计总支出
                queryItems(items, EXPAND);
                break;
            case '4':
                cout << "\n确认退出？(Y/N)";
                if (readQuitConfirm()) quit = true;
                break;
            default:
                "键盘输入错误";
        }
        cout << endl;
    }
}

void queryItems(const vector<AccountItem>& items) {
    cout << "---------查询结果--------------";
    double total;
    for(const AccountItem& item : items) {
        total += item.amount;
    }
    cout << "总收支："<< total << endl;
    cout << "按回车键返回主菜单" << endl;

    string line;
    getline(cin, line);
}

//总收入
void queryItems(const vector<AccountItem>& items, const string itemType) {
    cout << "---------查询结果--------------";
    double total;
    for(const AccountItem& item : items) {
        if (item.itemType == itemType) {
            total += item.amount;
        }
    }
    cout << ((itemType == INCOME) ? "总收入" : "总支出") << total << endl;
    cout << "按回车键返回主菜单" << endl;

    string line;
    getline(cin, line);
}